load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test", "rust_test_suite")

DEPENDENCIES = [
    # Keep sorted.
    "//rs/config",
    "//rs/http_endpoints/async_utils",
    "//rs/https_outcalls/service",
    "//rs/monitoring/logger",
    "//rs/monitoring/metrics",
    "@crate_index//:byte-unit",
    "@crate_index//:futures",
    "@crate_index//:http",
    "@crate_index//:http-body-util",
    "@crate_index//:hyper",
    "@crate_index//:hyper-rustls",
    "@crate_index//:hyper-socks2",
    "@crate_index//:hyper-util",
    "@crate_index//:parking_lot",
    "@crate_index//:prometheus",
    "@crate_index//:rand",
    "@crate_index//:serde",
    "@crate_index//:serde_json",
    "@crate_index//:slog",
    "@crate_index//:thiserror",
    "@crate_index//:tokio",
    "@crate_index//:tonic",
    "@crate_index//:tower",
]

MACRO_DEPENDENCIES = []

DEV_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:async-stream",
    "@crate_index//:bytes",
    "@crate_index//:once_cell",
    "@crate_index//:rstest",
    "@crate_index//:rustls",
    "@crate_index//:rustls-pemfile",
    "@crate_index//:socks5-impl",
    "@crate_index//:tempfile",
    "@crate_index//:tokio-rustls",
    "@crate_index//:uuid",
    "@crate_index//:warp",
]

MACRO_DEV_DEPENDENCIES = []

rust_library(
    name = "adapter",
    srcs = glob(
        ["src/**"],
        exclude = [
            "src/main.rs",
            "src/cli.rs",
        ],
    ),
    crate_name = "ic_https_outcalls_adapter",
    proc_macro_deps = MACRO_DEPENDENCIES,
    deps = DEPENDENCIES,
)

# Same target as above but allows the adapter to make HTTP calls.
rust_library(
    name = "adapter_with_http",
    testonly = True,
    srcs = glob(
        ["src/**"],
        exclude = [
            "src/main.rs",
            "src/cli.rs",
        ],
    ),
    crate_features = ["http"],
    crate_name = "ic_https_outcalls_adapter",
    proc_macro_deps = MACRO_DEPENDENCIES,
    visibility = ["//rs/pocket_ic_server:__subpackages__"],
    deps = DEPENDENCIES,
)

rust_binary(
    name = "ic-https-outcalls-adapter",
    srcs = [
        "src/cli.rs",
        "src/main.rs",
    ],
    proc_macro_deps = MACRO_DEPENDENCIES,
    visibility = [
        "//ic-os/guestos:__subpackages__",
        "//rs:release-pkg",
    ],
    deps = DEPENDENCIES + [
        ":adapter",
        "//rs/monitoring/adapter_metrics/server",
        "@crate_index//:clap",
    ],
)

# Same target as above but allows the adapter to make HTTP calls.
# This target is used for local testing (e.g. DFX)
rust_binary(
    name = "ic-outcalls-adapter-with-http",
    testonly = True,
    srcs = [
        "src/cli.rs",
        "src/main.rs",
    ],
    proc_macro_deps = MACRO_DEPENDENCIES,
    visibility = ["//rs:release-pkg"],
    deps = DEPENDENCIES + [
        ":adapter_with_http",
        "//rs/monitoring/adapter_metrics/server",
        "@crate_index//:clap",
    ],
)

rust_test(
    name = "adapter_test",
    crate = ":adapter",
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    deps = DEPENDENCIES + DEV_DEPENDENCIES,
)

rust_test(
    name = "adapter_main_test",
    crate = ":ic-https-outcalls-adapter",
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    deps = DEPENDENCIES + DEV_DEPENDENCIES,
)

rust_test_suite(
    name = "adapter_integration",
    srcs = glob(["tests/**/*.rs"]),
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    tags = ["requires-network"],
    deps = [":adapter"] + DEPENDENCIES + DEV_DEPENDENCIES,
)

rust_test_suite(
    name = "adapter_integration_with_http",
    srcs = glob(["tests/**/*.rs"]),
    crate_features = ["http"],
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    tags = ["requires-network"],
    deps = [":adapter_with_http"] + DEPENDENCIES + DEV_DEPENDENCIES,
)
